﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Application.SystemManage;
using ZFine.Code;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ZFine.Domain.Entity;

namespace ZFine.Web.Areas.SystemManage.Controllers
{
    public class ItemsTypeController : ControllerBase
    {
        private ItemsApp itemsApp = new ItemsApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeSelectJson()
        {
            var data = itemsApp.GetList();
            var treeList = new List<TreeSelectModel>();
            foreach (ItemsEntity item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.F_Id;
                treeModel.text = item.F_FullName;
                treeModel.parentId = item.F_ParentId;
                treeList.Add(treeModel);
            }
            return Content(treeList.TreeSelectJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeJson()
        {
            var data = itemsApp.GetList();
            var treeList = new List<TreeViewModel>();
            foreach (ItemsEntity item in data)
            {
                TreeViewModel tree = new TreeViewModel();
                bool hasChildren = data.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
                tree.id = item.F_Id;
                tree.text = item.F_FullName;
                tree.value = item.F_EnCode;
                tree.parentId = item.F_ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeViewJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeGridJson(string keyword)
        {
            var data = itemsApp.GetList(keyword);
            var treeList = new List<TreeGridModel>();
            List<string> parentList = new List<string>();
            foreach (ItemsEntity item in data)
            {
                TreeGridModel treeModel = new TreeGridModel();
                bool hasChildren = data.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
                treeModel.id = item.F_Id;
                treeModel.isLeaf = hasChildren;
                treeModel.parentId = item.F_ParentId;
                treeModel.expanded = hasChildren;
                treeModel.entityJson = item.ToJson();
                treeList.Add(treeModel);
                if (item.F_ParentId != null && !string.IsNullOrEmpty(item.F_ParentId))
                {
                    if (!parentList.Exists(f => f.Equals(item.F_ParentId)))
                    {
                        parentList.Add(item.F_ParentId);
                    }
                }
            }
            #region 张斌添加

            if (parentList != null && parentList.Count > 0)
            {
                parentList.ForEach(f =>
                {
                    var itemparent = itemsApp.GetForm(f);
                    if (itemparent != null)
                    {
                        TreeGridModel thistreeModel = new TreeGridModel();
                        bool hasChildren = data.Count(t => t.F_ParentId == itemparent.F_Id) == 0 ? false : true;
                        thistreeModel.id = itemparent.F_Id;
                        thistreeModel.isLeaf = hasChildren;
                        thistreeModel.parentId = itemparent.F_ParentId;
                        thistreeModel.expanded = hasChildren;
                        thistreeModel.entityJson = itemparent.ToJson();
                        treeList.Add(thistreeModel);
                    }
                });
            } 
            #endregion
            return Content(treeList.TreeGridJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = itemsApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(ItemsEntity itemsEntity, string keyValue)
        {
            itemsApp.SubmitForm(itemsEntity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            itemsApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
    }
}
